
package cn.xiqi.cms.hibernate.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiqi.cms.hibernate.DAO.CominfoDAO;
import cn.xiqi.cms.hibernate.beans.Cominfo;

public class ComServiceImpl implements ComService {
	private CominfoDAO dao;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Cominfo> getAll() {
		List<Cominfo> records;
		Map values = new HashMap<>();
		
		values.put("type", "all");
		records = dao.select(values);
		return records;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Cominfo getByID(int id) {
		Cominfo record;
		Map values = new HashMap<>();
		
		values.put("type", "id");
		values.put("id", id);
		record = dao.select(values).get(0);
		return record;
	}

	@Override
	public int add(Cominfo record) {
		int id;
		
		id = dao.insert(record);
		return id;
	}

	@Override
	public boolean delete(Cominfo record) {
		boolean b;
		
		b = dao.delete(record);
		return b;
	}

	@Override
	public boolean changeName(int id, String name) {
		Cominfo record;
		boolean b;
		
		record = getByID(id);
		record.setName(name);
		b = dao.update(record);
		return b;
	}

	@Override
	public boolean setRouter(int id, String router) {
		Cominfo record;
		boolean b;
		
		record = getByID(id);
		record.setRouter(router);
		b = dao.update(record);
		return b;
	}

	@Override
	public String getRouter(int id) {
		Cominfo record;
		String router;
		
		record = getByID(id);
		router = record.getRouter();
		return router;
	}

	@Override
	public boolean setRank(int id, int rank) {
		Cominfo record;
		boolean b;
		
		record = getByID(id);
		record.setRank(rank);
		b = dao.update(record);
		return b;
	}

	@Override
	public void setDao(CominfoDAO dao) {
		this.dao = dao;
	}

}
